/********************************************************************
	created:	30:9:2007   16:13
	author:		tiamo
	purpose:	entry point
*********************************************************************/

#include "stdafx.h"

#ifdef ALLOC_PRAGMA
	#pragma alloc_text(INIT,DriverEntry)
	#pragma alloc_text(PAGE,AppleKeyboardUnload)
#endif

// reg path
UNICODE_STRING															g_regPath;

// driver entry
NTSTATUS DriverEntry(__in PDRIVER_OBJECT pDriverObject,__in PUNICODE_STRING pRegPath)
{
	PAGED_CODE();

	C_ASSERT(sizeof(CHidData) == 0x6c);
	C_ASSERT(sizeof(CDeviceExtension) == 0x200);

	NTSTATUS statusRet = STATUS_SUCCESS;

	AppleDebugPrint(DBGLEVEL_INFO,"Enter AppleKeyboard DriverEntry %p,%p\n",pDriverObject,pRegPath);

	pDriverObject->MajorFunction[IRP_MJ_CREATE] = AppleKeyboardCreate;
	pDriverObject->MajorFunction[IRP_MJ_CLOSE] = AppleKeyboardClose;
	pDriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = AppleKeyboardIoControl;
	pDriverObject->MajorFunction[IRP_MJ_INTERNAL_DEVICE_CONTROL] = AppleKeyboardInternalIoControl;
	pDriverObject->MajorFunction[IRP_MJ_PNP] = AppleKeyboardPnp;
	pDriverObject->MajorFunction[IRP_MJ_FLUSH_BUFFERS] = AppleKeyboardFlush;
	pDriverObject->MajorFunction[IRP_MJ_POWER] = AppleKeyboardPower;
	pDriverObject->MajorFunction[IRP_MJ_SYSTEM_CONTROL] = AppleKeyboardSystemControl;
	pDriverObject->DriverUnload = AppleKeyboardUnload;
	pDriverObject->DriverExtension->AddDevice = AppleKeyboardAddDevice;

	g_regPath.Length = pRegPath->Length;
	g_regPath.MaximumLength = g_regPath.Length + sizeof(WCHAR);
	g_regPath.Buffer = static_cast<PWCH>(ExAllocatePoolWithTag(PagedPool,g_regPath.MaximumLength,'Appl'));
	if(g_regPath.Buffer)
		RtlCopyMemory(g_regPath.Buffer,pRegPath->Buffer,pRegPath->Length);
	else
		statusRet = STATUS_INSUFFICIENT_RESOURCES;

	AppleDebugPrint(DBGLEVEL_INFO,"Leave DriverEntry 0x%08x\n",statusRet);

	return statusRet;
}

// unload
VOID AppleKeyboardUnload(__in PDRIVER_OBJECT pDriverObject)
{
	PAGED_CODE();

	AppleDebugPrint(DBGLEVEL_INFO,"Enter AppleKeyboardUnload %p\n",pDriverObject);

	ASSERT(!pDriverObject->DeviceObject);

	ExFreePool(g_regPath.Buffer);

	AppleDebugPrint(DBGLEVEL_INFO,"Enter AppleKeyboardUnload\n");
}